package com.cinematicketscaning.app.controller.base

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import com.cinematicketscaning.app.widget.CountTimer
import android.view.WindowManager
import android.os.Build
import com.cinematicketscaning.app.R
import android.app.Activity
import android.view.View


open class BaseActivity: AppCompatActivity() {
    var countTimer: CountTimer? = null
    var broadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if(Intent.ACTION_TIME_TICK == intent.action){
                updateTime()
            }else if(Intent.ACTION_TIME_CHANGED == intent.action){
                updateTime()
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        countTimer = CountTimer(this, 100000, 1000)
        setStatusBarColor(this, R.color.white)
    }

    private fun registerTimeReceiver() {
        val filter = IntentFilter()
        filter.addAction(Intent.ACTION_TIME_TICK)
        filter.addAction(Intent.ACTION_TIME_CHANGED)
        registerReceiver(broadcastReceiver, filter)
        updateTime()
    }

    open fun isUseTimeReceiver(): Boolean {
        return false
    }

    open fun updateTime() {

    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        when (ev?.action) {
            MotionEvent.ACTION_UP -> {
                countTimer?.start()
            }
            else -> {
                countTimer?.cancel()
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onResume() {
        super.onResume()
        countTimer?.start()
        if (isUseTimeReceiver()) {
            registerTimeReceiver()
        }
    }

    override fun onPause() {
        super.onPause()
        countTimer?.cancel()
        if (isUseTimeReceiver()) {
            unregisterReceiver(broadcastReceiver)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (countTimer != null) {
            countTimer = null
        }
    }

    /**
     * 修改状态栏颜色，支持4.4以上版本
     * @param activity
     * @param colorId
     */
    fun setStatusBarColor(activity: Activity, colorId: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = activity.window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
            window.statusBarColor = activity.resources.getColor(colorId)
        }
    }
}